﻿using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigChannelsCtrlViewModel : BaseViewModel<ConfigChannelsCtrlViewModel>
    {
        #region Variables

        private string _seachValue = "";

        private ObservableCollection<ConfigChannelsModel> _disConfigChannelsModel = new ObservableCollection<ConfigChannelsModel>();
        #endregion

        #region Constructor

        public ConfigChannelsCtrlViewModel()
        {
        }

        #endregion

        #region Properties
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }
            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisConfigChannelsModel");

            }
        }
        public ObservableCollection<ConfigChannelsModel> DisConfigChannelsModel
        {
            get
            {
                _disConfigChannelsModel.Clear();
                List<ConfigChannelsModel> lst = DataManager.Instance.ConfigChannelsModelList.ToList().Where(O => O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _disConfigChannelsModel.Add(item);
                }
                return _disConfigChannelsModel;
            }

            set
            {
                _disConfigChannelsModel = value;
                RaisePropertyChanged("DisConfigChannelsModel");
            }
        }

        
        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void AddConfigChannelsModel(ConfigChannelsModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigChannelsService.Instance.Update(obj);
                ConfigChannelsDataGridViewModel.Instance.ConfigChannelsModels.Add(obj);
            }
            else ConfigChannelsService.Instance.Insert(obj);
        }
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigChannelsService.Instance.Update(sender as ConfigChannelsModel);
        }


        #endregion

        #region Public/Protected Methods
        public void DelConfigChannelsModel(ConfigChannelsModel configChannelsModel)
        {
            if (configChannelsModel != null)
            {
                ConfigChannelsDataGridViewModel.Instance.ConfigChannelsModels.Remove(configChannelsModel);
                RaisePropertyChanged("DisConfigChannelsModel");
                ConfigChannelsService.Instance.DeleteById(configChannelsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigChannelsModel model = new ConfigChannelsModel();
            DisConfigChannelsModel.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigChannelsModel(sender as ConfigChannelsModel);
        }
        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
